﻿using Tetris.Game.Common;
using Tetris.Game.ControlsBase;
using Tetris.Game.ControlsBase.Common;
using Tetris.Game.View;

namespace Tetris.Game
{
    public partial class FrmPlay : FrmBase
    {
        const int ROW = 20;
        const int COLUMN = 10;
        public FrmPlay()
        {
            InitializeComponent();
            BackColor = Color.Transparent;

            GridSize = Math.Min(this.Width / COLUMN, this.Height / ROW); // 每个网格的大小
        }
        public int GridSize { get; private set; }
        public Action CountDownEndCallback { get; set; }
        public unsafe void ArrangeBlock(BlockControl[] blocks, bool isRemove)
        {
            this.ExecBeginInvoke(() =>
            {
                fixed (BlockControl* block = blocks)
                {
                    for (int i = 0; i < blocks.Length; i++)
                    {
                        BlockControl* cur = block + i;
                        if (*cur == null) continue;
                        if (isRemove)
                        {
                            cur->Visible = false;
                            this.Controls.Remove(*cur);
                            cur->Dispose();
                            (*cur) = null;
                        }
                        else
                            cur->UpdateLocation();
                    }
                }
            });
        }
        public void InitData(BlockControl[,] blockControls)
        {
            this.ExecBeginInvoke(() =>
            {
                if (blockControls != null)
                {
                    int row = blockControls.GetLength(0);
                    int col = blockControls.GetLength(1);

                    if (blockControls != null)
                    {
                        for (int i = 0; i < row; i++)
                        {
                            for (int j = 0; j < col; j++)
                            {
                                var block = blockControls[i, j];
                                block.DisposeEx();
                            }
                        }
                    }
                }

                this.Controls.Clear();
            });
        }
        public Action<DirectionType> MoveCallback { get; set; }
        public Action<KeystrokeType> KeystrokeCallback { get; set; }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Up)
            {
                MoveCallback?.Invoke(DirectionType.Up);
                KeystrokeCallback?.Invoke(KeystrokeType.Up);
                // 处理向上键的逻辑
                return true; // 返回true表示已处理该键盘输入
            }
            else if (keyData == Keys.Down)
            {
                // 处理向下键的逻辑
                MoveCallback?.Invoke(DirectionType.Down);
                KeystrokeCallback?.Invoke(KeystrokeType.Down);
                return true;
            }
            else if (keyData == Keys.Left)
            {
                // 处理向左键的逻辑
                MoveCallback?.Invoke(DirectionType.Left);
                KeystrokeCallback?.Invoke(KeystrokeType.Left);
                return true;
            }
            else if (keyData == Keys.Right)
            {
                // 处理向右键的逻辑
                MoveCallback?.Invoke(DirectionType.Right);
                KeystrokeCallback?.Invoke(KeystrokeType.Right);
                return true;
            }
            else if (keyData == Keys.Space)
            {
                // 处理空格键的逻辑
                MoveCallback?.Invoke(DirectionType.Space);
                KeystrokeCallback?.Invoke(KeystrokeType.Space);
                return true;
            }
            else if (keyData == Keys.Enter)
            {
                MoveCallback?.Invoke(DirectionType.Enter);
                KeystrokeCallback?.Invoke(KeystrokeType.Enter);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            using var brush = new SolidBrush(Color.FromArgb(30, 217, 236, 255));
            e.Graphics.FillRectangle(brush, ClientRectangle);

            using var pen = new Pen(Color.FromArgb(30, 255, 255, 255), 1);

            int offsetX = (this.Width - GridSize * COLUMN) / 2; // 横坐标偏移量
            int offsetY = (this.Height - GridSize * ROW) / 2; // 纵坐标偏移量

            // 绘制垂直网格线
            for (int x = 0; x <= COLUMN; x++)
            {
                int xPos = x * GridSize + offsetX;
                e.Graphics.DrawLine(pen, xPos, 0, xPos, this.Height);
            }

            // 绘制水平网格线
            for (int y = 0; y <= ROW; y++)
            {
                int yPos = y * GridSize + offsetY;
                e.Graphics.DrawLine(pen, 0, yPos, this.Width, yPos);
            }

            // 绘制垂直网格线
            //for (int x = 0; x <= COLUMN; x++)
            //{
            //    int xPos = x * gridSize;
            //    e.Graphics.DrawLine(pen, xPos, 0, xPos, ClientSize.Height);
            //}

            //// 绘制水平网格线
            //for (int y = 0; y <= ROW; y++)
            //{
            //    int yPos = y * gridSize;
            //    e.Graphics.DrawLine(pen, 0, yPos, ClientSize.Width, yPos);
            //}
        }
        public unsafe void PaintBlockControl(BlockControl[] blockControls)
        {
            this.ExecBeginInvoke(() =>
            {
                fixed (BlockControl* blockControl = blockControls)
                {
                    for (int i = 0; i < blockControls.Length; i++)
                    {
                        BlockControl* cur = blockControl + i;
                        this.Controls.Add(*cur);
                        cur->UpdateLocation();
                    }
                }
            });
        }
        public void UpdateLocation(BlockControl blockControl)
        {
            if (blockControl == null)
                return;

            this.ExecBeginInvoke(() =>
            {
                blockControl.UpdateLocation();
            });
        }
    }
}
